package code.jam.connect;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.util.Log;

public class JamClient extends java.lang.Object implements Runnable
{
	private String server_ip = null;
	private int server_port = 0;
	
	
	public JamClient(String server_ip, int server_port) 
	{
		this.server_ip = server_ip;
		this.server_port = server_port;
	}
	public static final String	DEBUG_TAG	= "JamClient";

	public void run()
	{
		try
		{
			byte[] ip = JamClient.toByte( server_ip );	
			
			InetAddress address = InetAddress.getByAddress( ip );

//			Socket socket = new Socket(address, server_port);
			
			LocalSocket socket = new LocalSocket();
			
//			socket.bind(new LocalSocketAddress("192.168.10.104"));
			
			socket.connect(new LocalSocketAddress("192.168.10.105"));
			
			String Message = " merong ";

			OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());

			BufferedWriter bw = new BufferedWriter(osw);

			PrintWriter out = new PrintWriter(bw, true);

			out.println(Message);

		}
		catch (UnknownHostException e)
		{
			Log.e(DEBUG_TAG, e.getMessage());
			e.printStackTrace();
		}
		catch (IOException e)
		{
			Log.e(DEBUG_TAG, e.getMessage());
			e.printStackTrace();
		}
	}

	public static byte[] toByte( String source)
	{

		String[] strings = source.split("\\.");

		byte[] ip = new byte[4];

		for(int i = 0; i < strings.length; i++)
		{
			ip[i] = new Integer(strings[i]).byteValue();
		}

		return ip;
	}
}
